<template>
  <div class="main_spac1 maincl">
    <div class="main_white padding10">
      <div class="main_crumbs boxdq">
        <div class="main_crumbs_l">
          <a href="javascript:;" @click="$router.back(-1)">
            <i class="el-icon-arrow-down"></i>返回
          </a>
          <em>|</em>
          <span>超级管理员账号</span>
        </div>
      </div>
    </div>

    <div class="main_table">
      <div class="main_content" style="min-height: 550px">
        <el-table v-loading="listLoading" :data="list" element-loading-text="加载中" ref="multipleTable" fit border
          highlight-current-row>
          <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true" />
          <el-table-column label="用户名" prop="username" :show-overflow-tooltip="true" />
          <el-table-column label="手机" prop="mobile" :show-overflow-tooltip="true" />
          <el-table-column label="所属角色" prop="role.name" :show-overflow-tooltip="true" />
          <el-table-column label="状态" prop="status" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <div :class="[
                { green: scope.row.status == 1 },
                { red: scope.row.status == 0 }
              ]">
                {{ scope.row.status == 1 ? "正常" : "禁用" }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link style="color:#666666" type="primary" @click="avoidClose(scope)">免密登陆</el-link>
              <el-link style="margin-left:5px" type="primary" @click="changePass(scope.row.id)">修改密码</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination :current-page="current.page" :page-sizes="[20, 30, 50, 100]" :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper" :total="totalCount" background @size-change="changeLimit"
            @current-change="changePage" />
        </div>
      </div>
    </div>

    <el-dialog title="修改密码" :close-on-click-modal="false" :visible.sync="dialogVisible" width="35%">
      <el-form ref="passform" :model="formData" @submit.native.prevent label-width="120px" :rules="rules">
        <el-form-item label="密码" prop="newpass">
          <el-input v-model.trim="formData.newpass" placeholder="请输入密码" v-model="formData.newpass" autocomplete="off"
            @input="inputFun" show-password></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="nextpass">
          <el-input v-model.trim="formData.nextpass" placeholder="请输入密码" v-model="formData.nextpass" @input="inputFun1"
            autocomplete="off" show-password></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onsubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Debounce } from '@/utils/index.js';
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import {
  administratorByComId,
  changePassword,
  loginNoPass
} from "@/api/order/userList";
export default {
  data() {
    return {
      current: {
        page: 1,
        per_page: 20
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: [],
      listLoading: false,
      dialogVisible: false, //修改密码弹窗
      user_id: "", //修改密码点击提交传的用户id值
      formData: {
        newpass: "", //新密码
        nextpass: "" //再次输入密码
      },

      rules: {
        newpass: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur"
          }
        ],
        nextpass: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.order_index();
    },
    changePage(page) {
      this.current.page = page;
      this.order_index();
    },
    //点击修改密码弹窗
    changePass(id) {
      this.user_id = id;
      this.dialogVisible = true;
    },
    //禁止回车输入空格
    inputFun(e) {
      this.$nextTick(() => {
        //正则过滤空格
        this.formData.newpass = e.replace(/\s+/g, "");
      });
    },
    //禁止回车输入空格
    inputFun1(e) {
      this.$nextTick(() => {
        //正则过滤空格
        this.formData.nextpass = e.replace(/\s+/g, "");
      });
    },
    //点击确认获取数据
    onsubmit() {
      this.$refs["passform"].validate(valid => {
        if (this.formData.newpass != this.formData.nextpass) {
          this.$message.error("两次密码输入不一致");
        } else {
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
              )
            },
            apiUrl().name,
            { user_id: this.user_id, password: this.formData.nextpass }
          );
          changePassword(data).then(res => {
            if (res.errcode == 0) {
              this.$message({
                message: "密码修改成功",
                type: "success"
              });
              this.dialogVisible = false;
              this.formData.newpass = "";
              this.formData.nextpass = "";
            }
          });
        }
      });
    },
    //免密登陆
    avoidClose: Debounce(function (scope) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        { user_id: scope.row.id, school_id: this.$route.query.id }
      );
      loginNoPass(data).then(res => {
        if (res.errcode == 0) {
          window.open(`${res.data.url}#/?id=${res.data.token}`);
        } else {
          this.$message.error(res.errmsg);
        }
      });
    }),
    //获取初始数据
    getList(id) {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name,
        { school_id: id }
      );
      administratorByComId(data).then(res => {
        console.log(res);
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data.list;
        }
      });
    }
  },
  mounted() {
    let id = this.$route.query.id;
    this.getList(id);
  }
};
</script>

<style lang="scss" scoped>
// @import "@/styles/variables.scss";

.main_table {
  margin-top: 0px;
  height: auto;
  background-color: white;
  padding-bottom: 15px;
}

.main_content {
  padding: 15px;
}

.green {
  color: #03a98e;
}

.red {
  color: #f56c6c;
}

.passtxt {
  padding-bottom: 10px;
  font-size: 16px;
}

.lf-m-t15 {
  margin-top: 15px;
}

.company {
  margin-right: 5px;
  text-align: center;
}

.maincl {
  .el-select {
    width: 114px;
  }

  .more {
    margin-left: 50px;
  }

  .handle_btn {
    padding: 0;
    border: 0;
  }

  .edit_dialog {
    .address {
      max-width: 350px;
    }
  }

  .mainor_form form {
    // width: calc(100% - 180px);
    width: 100%;
  }

  .main_table /deep/.el-table--small td {
    padding: 10px 0;
  }

  .main_table /deep/.el-table--small th {
    padding: 10px 0;
  }

  /deep/.el-table {
    tr {
      &:hover {
        background: none !important;
      }

      &.hover-row>td {
        background: none !important;
      }

      .current-row>td {
        background: none !important;
      }
    }
  }

  /deep/.el-table .el-table__body tr:hover {
    background: #fff !important;
  }

  /deep/.el-table .el-table__body tr:hover>td {
    background: #fff !important;
  }

  /deep/.el-table tr.expanded+tr {
    background: #fafafa !important;
  }

  /deep/.el-table tr.expanded+tr>td {
    background: #fafafa !important;
  }

  /deep/.el-table tr.expanded+tr .mainex_ta {
    background: #fafafa !important;
  }

  /deep/.el-table tr.expanded+tr .mainex_ta th,
  /deep/.el-table tr.expanded+tr .mainex_ta tr {
    background: #fafafa !important;
  }

  /deep/.el-table tr.expanded+tr:hover {
    background: #fafafa !important;
  }

  /deep/.el-table tr.expanded+tr:hover>td {
    background: #fafafa !important;
  }

  /deep/.el-table tr.expanded+tr .el-table tr:hover {
    background: #fafafa !important;
  }

  /deep/.el-table tr.expanded+tr .el-table tr:hover>td {
    background: #fafafa !important;
  }

  /deep/.el-form-item__label {
    width: auto !important;
  }

  /deep/.main_table .el-table--small td .cell {
    height: auto;
    overflow: auto;
    white-space: normal !important;
  }
}

/deep/.main_page {
  margin: 0px !important;
  padding: 0px 15px !important;
}
</style>
